//引入express模块
const express=require('express');
//引入连接池对象
const pool=require('../pool.js');
//创建路由器对象
const a=express.Router();
//1.添加用户地址
a.post('/',(req,res,next)=>{
  //1.1获取post请求的数据
  let obj=req.body;
  //1.2循环验证
  let i=400;
  for (let k in obj){
	i++;
    if(!obj[k]){
	  res.send({code:i,msg:k+'不能为空'});
	  return;
	}
  }
  //1.3验证isdefault格式
//   if(obj.isdefault!=0&&obj.isdefault!=1){
//     res.send({code:301,msg:'isdefault格式不正确'});
// 	return;
//   }
  //1.4验证手机格式
  if(!(/^1[3-9]\d{9}$/.test(obj.tel))){
    res.send({code:302,msg:'cellphone格式不正确'});
  }
  //1.5执行SQL命令
  pool.query('insert into tm_receiver_address set?',[obj],(err,result)=>{
    if(err){
	  next(err);
	  return;
	}
	if(result.affectedRows===0){
	  res.send({code:201,msg:'添加失败'});
	}else{
	  res.send({code:200,msg:'添加成功'});
	}
  });
});
//2.删除用户地址
a.delete('/:aid',(req,res,next)=>{
	//获取路由传参的数据
	let obj=req.params;
	console.log(obj.aid);
	//执行SQL命令
	pool.query('delete from tm_receiver_address where aid=?',[obj.aid],(err,result)=>{
		if(err){
			console.log(err);
			next(err);
			return;
		}
		if(result.affectedRows===0){
			res.send({code:201,msg:'删除失败'});
		}else{
			res.send({code:200,msg:'删除成功'});
		}
	})
})
//3.修改用户地址
a.put('/',(req,res,next)=>{
  //3.1获取传递的数据
  let obj=req.body;
  //3.2循环验证
  let i=400;
  for(let k in obj){
    i++;
	if(!obj[k]){
	  res.send({code:i,msg:k+'不能为空'});
	  return;
	}
  }
  //3.3验证手机格式
  if(!/^1[3-9]\d{9}$/.test(obj.tel)){
    res.send({code:301,msg:'手机号码格式错误'});
	return;
  }
  //3.5执行SQL命令
  pool.query('update tm_receiver_address set?where aid=?',[obj,obj.aid],(err,result)=>{
	  console.log(err);
    if(err){
	  next(err);
	  return;
	}
	if(result.affectedRows===0){
	  res.send({code:201,msg:'修改失败'});
	}else{
	  res.send({code:200,msg:'修改成功'});
	}
  });
})
//4.查询地址列表
a.get('/',(req,res,next)=>{
  //4.1获取查询字符串的数据
  let obj=req.query;
  //4.3执行SQL命令
  pool.query('select * from tm_receiver_address where user_id=?',[obj.uid],(err,result)=>{
	  console.log(err);
    if(err){
	  next(err);
	  return;
	}
	res.send({
	  code:200,
	  msg:'查询成功',
	  data:result
	});
  });
});
//5.按照地址编号查询地址列表
a.get('/ad',(req,res,next)=>{
	//4.1获取查询字符串的数据
	let obj=req.query;
	//4.3执行SQL命令
	pool.query('select * from tm_receiver_address where user_id=?',[obj.uid],(err,result)=>{
		console.log(err);
	  if(err){
		next(err);
		return;
	  }
	  res.send({
		code:200,
		msg:'查询成功',
		data:result
	  });
	});
  });
  //6.查询用户默认地址
a.get('/default',(req,res,next)=>{
	//4.1获取查询字符串的数据
	let obj=req.query;
	//4.3执行SQL命令
	pool.query('select * from tm_receiver_address where user_id=? and isDefault=1',[obj.uid],(err,result)=>{
		console.log(err);
	  if(err){
		next(err);
		return;
	  }
	  res.send({
		code:200,
		msg:'查询成功',
		data:result
	  });
	});
  });
//   7.修改默认地址为不默认
a.put('/de',(req,res,next)=>{
	//3.1获取传递的数据
	let obj=req.body;
	//3.5执行SQL命令
	pool.query('update tm_receiver_address set isDefault = 0 where isDefault=1 and user_id=?',[obj.uid],(err,result)=>{
		console.log(err);
	  if(err){
		next(err);
		return;
	  }
	  if(result.affectedRows===0){
		res.send({code:201,msg:'修改失败'});
	  }else{
		res.send({code:200,msg:'修改成功'});
	  }
	});
  })
  //   8.修改为默认地址
a.put('/unde',(req,res,next)=>{
	//3.1获取传递的数据
	let obj=req.body;
	//3.5执行SQL命令
	pool.query('update tm_receiver_address set isDefault = 1 where aid=? and user_id=?',[obj.aid,obj.uid],(err,result)=>{
		console.log(err);
	  if(err){
		next(err);
		return;
	  }
	  if(result.affectedRows===0){
		res.send({code:201,msg:'修改失败'});
	  }else{
		res.send({code:200,msg:'修改成功'});
	  }
	});
  })
//导出路由器
module.exports=a;



